package net.csdn.business.common.oauth2.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.constant.GatewayConstants;
import net.csdn.business.common.domain.common.RouteEvent;
import net.csdn.business.common.domain.request.oauth.GatewayRouteInfoQuery;
import net.csdn.business.common.domain.request.oauth.NewGatewayRouteInfoDTO;
import net.csdn.business.common.domain.request.oauth.UpdateGatewayRouteInfoDTO;
import net.csdn.business.common.domain.vo.oauth.GatewayRouteInfoVO;
import net.csdn.business.common.enums.ResultCodeEnum;
import net.csdn.business.common.exception.BusinessException;
import net.csdn.business.common.oauth2.mapper.GatewayRouteInfoMapper;
import net.csdn.business.common.oauth2.model.entity.GatewayRouteInfo;
import net.csdn.business.common.oauth2.service.IGatewayRouteInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 网关路由信息Service业务层处理
 * 
 * @author zhangyl
 * @date 2022-12-13
 */
@Slf4j
@Service
public class GatewayRouteInfoServiceImpl extends ServiceImpl<GatewayRouteInfoMapper, GatewayRouteInfo> implements IGatewayRouteInfoService 
{

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 分页查询网关路由信息列表
     *
     * @param query 网关路由信息
     * @return 网关路由信息
     */
    @Override
    public Page<GatewayRouteInfoVO> findPageList(GatewayRouteInfoQuery query) {
        //获得Page对象
        Page<GatewayRouteInfoVO> page = query.getPage();
        List<GatewayRouteInfoVO> pageList=baseMapper.findPageList(page,query);
        page.setRecords(pageList);
        return page;
    }

    /**
     * 根据id查询网关路由信息
     * 
     * @param id 网关路由信息主键
     * @return 网关路由信息
     */
    @Override
    public GatewayRouteInfoVO getById(Long id)
    {
        return baseMapper.selectGatewayRouteInfoById(id);
    }

    @Override
    public GatewayRouteInfo selectByRouteId(String routeId) {
        LambdaQueryWrapper<GatewayRouteInfo> queryWrapper = new QueryWrapper<GatewayRouteInfo>().lambda();
        queryWrapper.eq(GatewayRouteInfo::getRouteId, routeId);
        GatewayRouteInfo routeInfo=baseMapper.selectOne(queryWrapper);
        return routeInfo;
    }

    /**
     * 查询所有网关路由信息列表
     *
     * @return 网关路由信息
     */
    @Override
    public List<GatewayRouteInfo> selectGatewayRouteInfoList()
    {
        return baseMapper.selectGatewayRouteInfoList();
    }

    /**
     * 新增网关路由信息
     *
     * @param params 网关路由信息
     * @return 结果
     */
    @Override
    @Transactional
    public Integer addGatewayRoute(NewGatewayRouteInfoDTO params)
    {
        //查询路由id是否存在
        LambdaQueryWrapper<GatewayRouteInfo> queryWrapper = new QueryWrapper<GatewayRouteInfo>().lambda();
        queryWrapper.eq(GatewayRouteInfo::getRouteId, params.getRouteId());
        GatewayRouteInfo routeInfo=baseMapper.selectOne(queryWrapper);
        if(routeInfo!=null){
            throw new BusinessException(ResultCodeEnum.GATEWAY_ROUTE_ID_EXIST_ERROR);
        }
        GatewayRouteInfo gatewayRouteInfo=new GatewayRouteInfo();
        BeanUtils.copyProperties(params,gatewayRouteInfo);
        gatewayRouteInfo.setCreatedDate(new Date());
        gatewayRouteInfo.setCreatedBy(params.getCreatedBy());
        baseMapper.insert(gatewayRouteInfo);
        RouteEvent message=new RouteEvent();
        message.setRouteId(gatewayRouteInfo.getRouteId());
        message.setType(1);
        sendMessage(JSON.toJSONString(message));
        return 1;
    }

    /**
     * 修改网关路由信息
     *
     * @param params 网关路由信息
     * @return 结果
     */
    @Override
    @Transactional
    public Integer updateGatewayRoute(UpdateGatewayRouteInfoDTO params)
    {
        //查询路由id是否存在
        LambdaQueryWrapper<GatewayRouteInfo> queryWrapper = new QueryWrapper<GatewayRouteInfo>().lambda();
        queryWrapper.eq(GatewayRouteInfo::getId,params.getId());
        GatewayRouteInfo routeInfo=baseMapper.selectOne(queryWrapper);
        if(routeInfo==null){
            throw new BusinessException(ResultCodeEnum.GATEWAY_ROUTE_NO_EXIST_ERROR);
        }
        GatewayRouteInfo gatewayRouteInfo=new GatewayRouteInfo();
        BeanUtils.copyProperties(params,gatewayRouteInfo);
        gatewayRouteInfo.setLastModifiedDate(new Date());
        gatewayRouteInfo.setLastModifiedBy(params.getLastModifiedBy());
        baseMapper.updateById(gatewayRouteInfo);
        RouteEvent message=new RouteEvent();
        message.setRouteId(routeInfo.getRouteId());
        message.setType(2);
        sendMessage(JSON.toJSONString(message));
        return 1;
    }

    /**
     * 删除网关路由信息信息
     * 
     * @param id 网关路由信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public Integer deleteGatewayRoute(Long id)
    {
        GatewayRouteInfo gatewayRouteInfo=baseMapper.selectById(id);
        if(gatewayRouteInfo!=null){
            int res=baseMapper.deleteById(id);
            if(res>0){
                RouteEvent message=new RouteEvent();
                message.setRouteId(gatewayRouteInfo.getRouteId());
                message.setType(3);
                sendMessage(JSON.toJSONString(message));
                return 1;
            }
        }
       return 0;
    }


    public void sendMessage(String message){
        log.info("--------gateway route manage send message:{}",message);
        stringRedisTemplate.convertAndSend(GatewayConstants.ROUTE_SUB_CHANNEL, message);
    }
}
